<template>
  <el-dialog
    :title="!dataForm.receiveId ? '报修申请' : '报修申请'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  :rules="dataRule"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="资产名" prop="assetsName">
        <el-input v-model="dataForm.assetsName" placeholder="资产名" disabled></el-input>
      </el-form-item>
      <el-form-item label="资产编号" prop="assetsNum">
        <el-input v-model="dataForm.assetsNum" placeholder="资产编号" disabled></el-input>
      </el-form-item>
      <el-form-item label="报修人" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="报修人" disabled></el-input>
      </el-form-item>
      <el-form-item label="报修日期" prop="repairsTime">
        <el-date-picker v-model="dataForm.repairsTime" type="date" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="报修证明" prop="repairsPic" v-model="dataForm.repairsPic">
      <el-upload
        class="avatar-uploader"
        :action="url1"
        :show-file-list="false"
        :on-success="successHandle">
        <img v-if="dataForm.repairsPic" :src="dataForm.repairsPic" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </el-form-item>

      <el-form-item label="报修说明" prop="repairsDesc">
        <el-input v-model="dataForm.repairsDesc" placeholder="维修说明"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      url1: '',
      visible: false,
      dataForm: {
        receiveId: 0,
        repairsTime: '',  // 报修日期
        assetsName: '',
        assetsNum: '',
        userName: '',
        repairsPic: '',    // 附件
        repairsDesc: ''  // 报修说明
      },
      dataRule: {
        repairsTime: [
          { required: true, message: '维修日期不能为空', trigger: 'blur' }
        ],
        repairsDesc: [
          { required: true, message: '报修说明不能为空', trigger: 'blur' }
        ],
        repairsPic: [
          { required: true, message: '报修附件不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.url1 = this.$http.adornUrl(`/file/upload4?token=${this.$cookie.get('token')}`)
      this.dataForm.receiveId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.receiveId) {
          this.$http({
            url: this.$http.adornUrl(`/assets/receive/info/${this.dataForm.receiveId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.assetsName = data.receive.assetsName
              this.dataForm.assetsNum = data.receive.assetsNum
              this.dataForm.userName = data.receive.userName
              this.dataForm.receiveTime = data.receive.receiveTime
              this.dataForm.createBy = data.receive.createBy
              this.dataForm.createTime = data.receive.createTime
              this.dataForm.receiveDesc = data.receive.receiveDesc
              this.dataForm.repairsPic = data.receive.repairsPic
            }
          })
        }
      })
    },
    // 上传成功
    successHandle (response, file, fileList) {
      console.log(response)
      this.dataForm.repairsPic = response.assetsImg
      // eslint-disable-next-line no-undef
      console.log('地址  ' + dataForm.repairsPic)
      this.$message.success('上传成功')
    },
    //  获得当前时间
    getCurrentTime () {
      return new Date()
    },
    // 审核表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/assets/repairs/repairsApply'),
            method: 'post',
            data: this.$http.adornData({
              'repairsTime': this.getCurrentTime(), // 报修时间
              'userName': this.dataForm.userName,   // 报修人
              'repairsStatus': '0',   // 维修申请中  0:维修申请中 1:维修完成 2:维修中 3报废   4 拒绝维修
              'assetsNum': this.dataForm.assetsNum,   // 资产编号
              'assetsName': this.dataForm.assetsName,   // 资产名字
              'repairsDesc': this.dataForm.repairsDesc,  // 描述    未禁用
              'repairsPic': this.dataForm.repairsPic
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

